package com.stone.business.workcard.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.stone.business.gzrwd.pojo.TGzrwd;
import com.stone.business.gzrwd.pojo.TGzrwdRw_old;
import com.stone.business.plan.pojo.TPlanBz;
import com.stone.business.workcard.pojo.Gxzlkzk;
import com.stone.business.workcard.pojo.Gxzlkzk_BZ;
import com.stone.business.workcard.pojo.Gxzlkzk_GX;
import com.stone.business.workcard.pojo.MainWorkCardOne;
import com.stone.business.workcard.pojo.Ryzzfg;
import com.stone.business.workcard.service.WorkcardService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TMenu;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.utils.DateFormatTool;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class GxzlkzkAction extends BaseAction {
	private WorkcardService workcardService;
	private Gxzlkzk entity=new Gxzlkzk();
	private String id;
	private String bzgsids;
	private String modelids;
	private List<Gxzlkzk_BZ> gxzlkzkBZList;
	private List<Gxzlkzk_GX> gxzlkzkGXList;
	private String directFlag;
	// 父节点
	private String parentId;
	
	private String ocGx;
	
	private String ocGybzjyq;
	
	private String ocFxtx;
	private String sortId;
	private String start;
	
	private String limit;
	/**
	 * 初始化
	 * @return
	 */
	public String initGxzlkzk(){
		String sysdate = workcardService.getSysdate("yyyyMMdd");
		List<HashMap<String, Object>> list = workcardService.findListBySql("select MAX(serial_number) as bh from TAB_GXZLKZK where serial_number like '" + sysdate + "%'");
		
		// 票号取得
		if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
			this.entity.setSerial_number(sysdate + "0001");			
		} else {
			if(this.entity.getSerial_number()==null){
				//System.out.println("this.entity.getBh()=="+this.entity.getBh());
				
			this.entity.setSerial_number(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			}
		}
		//从工作任务单中提取信息
		if(!StringUtils.isNullOrBlankFull(id)) {
			TGzrwd tGzrwd = (TGzrwd)workcardService.findById(Long.parseLong(id), TGzrwd.class.getName());//取得工作任务单pojo
			this.entity.setOc_gzph(tGzrwd.getGzpno()); 	//设置票号
			this.entity.setOc_ecgzaqcsph("");			//设置二次工作安全措施票号			
			TUser tUser=(TUser)workcardService.findById(Long.parseLong(tGzrwd.getZbz().trim()), TUser.class.getName());//取得班组pojo
			this.entity.setOc_gzfzr(tUser.getName());	//设置工作负责人
			this.entity.setOc_cy(tGzrwd.getZbyname());	//设置成员
			TOrgan tOrgan=(TOrgan)workcardService.findById(Long.parseLong(tGzrwd.getBgzr().trim()), TOrgan.class.getName());//取得班组pojo
			this.entity.setOc_gzbz(tOrgan.getOrganName());	//设置工作班组
			this.entity.setDt_jhgzsjq(tGzrwd.getCzkssj());//设置计划工作时间起
			this.entity.setDt_jhgzsjz(tGzrwd.getCzjssj());//设置票号计划工作时间止
		}
		if(entity!=null&&"".equals(entity.getOc_bxr())||entity.getOc_bxr()==null){
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);		
			entity.setOc_bxr(tUser.getName());
			entity.setCjrid(tUser.getId());
		}
		if(directFlag==null||directFlag.equals("")){
			return "INIT_SUCCESS";
		}else{
			return directFlag;
		}
	}
	/**
	 * 初始化
	 * @return
	 */
	public String initDetail(){
		String sysdate = workcardService.getSysdate("yyyyMMdd");
		List<HashMap<String, Object>> list = workcardService.findListBySql("select MAX(serial_number) as bh from TAB_GXZLKZK where serial_number like '" + sysdate + "%'");
		
		// 票号取得
		if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
			this.entity.setSerial_number(sysdate + "0001");			
		} else {
			if(this.entity.getSerial_number()==null){
				//System.out.println("this.entity.getBh()=="+this.entity.getBh());
				
			this.entity.setSerial_number(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
			}
		}
		//从工作任务单中提取信息
		if(!StringUtils.isNullOrBlankFull(id)) {
			gxzlkzkBZList=workcardService.findListByHql("from Gxzlkzk_BZ where p_idsno='"+id+"' order by to_number(sort_id)");
			
		}
		if(entity!=null&&"".equals(entity.getOc_bxr())||entity.getOc_bxr()==null){
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);		
			entity.setOc_bxr(tUser.getName());
			entity.setCjrid(tUser.getId());
		}
		if(directFlag==null||directFlag.equals("")){
			return "INIT_SUCCESS";
		}else{
			return directFlag;
		}
	}
	/**
	 * 保存
	 * @return
	 */
	public String saveGxzlkzk(){
		entity.setDt_czrq(DateFormatTool.formatToDate(workcardService.getSysdate("yyyy-MM-dd HH24:mi"), true));
		TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);
		//entity.setOc_bxr(tUser.getName());
		entity.setOc_yxbz("T");
		//entity.setOc_status("2");	
		String info=workcardService.doSave(entity, gxzlkzkBZList);
		gxzlkzkBZList=workcardService.findListByHql("from Gxzlkzk_BZ where p_idsno='"+this.entity.getId_sno()+"' order by to_number(sort_id)");
		super.setInfo(info);
		if(directFlag==null||directFlag.equals("")){
		return "SAVE_SUCCESS";
		}else{
			return directFlag;
		}
	}
	/**
	 * 查看明细选择标准作业名称时刷新
	 * @return
	 */
	public String updateGxzlkzk(){
		if(!StringUtils.isNullOrBlankFull(id)) {
			this.entity = (Gxzlkzk)workcardService.findById(Long.parseLong(id), Gxzlkzk.class.getName());
			if(!StringUtils.isNullOrBlankFull(bzgsids)) {
				gxzlkzkGXList=workcardService.findListByHql("from Gxzlkzk_GX where parentId in ("+bzgsids+") order by sort_id");
				this.gxzlkzkBZList = new ArrayList<Gxzlkzk_BZ>();
				for (Gxzlkzk_GX gx : gxzlkzkGXList) {
					Gxzlkzk_BZ bz = new Gxzlkzk_BZ();
					bz.setId_sno(Long.parseLong("0"));
					bz.setOc_gx(gx.getOc_gx());
					bz.setOc_gybzjyq(gx.getOc_gybzjyq());
					bz.setOc_fxtx(gx.getOc_fxtx());
					bz.setSortId(gx.getSortId().toString());
					this.gxzlkzkBZList.add(bz);

				}
			}else if(!StringUtils.isNullOrBlankFull(modelids)) {
				gxzlkzkBZList=workcardService.findListByHql("from Gxzlkzk_BZ where p_idsno in ("+modelids+") order by to_number(sort_id)");
				
			}else{
			gxzlkzkBZList=workcardService.findListByHql("from Gxzlkzk_BZ where p_idsno='"+this.entity.getId_sno()+"' order by to_number(sort_id)");
			}
		}
		if(entity!=null&&entity.getOc_status()!=null){
		String sh_Name=((entity.getOc_status().equals("0"))?"审核不通过":(entity.getOc_status().equals("1")?"审核通过":"待审核"));
		entity.setOc_status_name(sh_Name);
		}
		
		if(directFlag==null||directFlag.equals("")){
			return "UPDATE_SUCCESS";
		}else{
			return directFlag;
		}
	}
	public String skipByPlanid(){
		
		String planid=this.entity.getPlanid();
		this.entity = (Gxzlkzk)workcardService.findFirst(" from Gxzlkzk t where t.planid='"+this.entity.getPlanid()+"'");
		if(this.entity!=null){
			gxzlkzkBZList=workcardService.findListByHql("from Gxzlkzk_BZ where p_idsno='"+this.entity.getId_sno()+"' order by to_number(sort_id)");
		}
		
		if(entity!=null&&entity.getOc_status()!=null){
			String sh_Name=((entity.getOc_status().equals("0"))?"审核不通过":(entity.getOc_status().equals("1")?"审核通过":"待审核"));
			entity.setOc_status_name(sh_Name);
			}
		
		
		if(entity==null){	
			entity=new Gxzlkzk();
			
			String sysdate = workcardService.getSysdate("yyyyMMdd");
			List<HashMap<String, Object>> list = workcardService.findListBySql("select MAX(serial_number) as bh from TAB_GXZLKZK where serial_number like '" + sysdate + "%'");
			
			// 票号取得
			if (list.get(0).get("bh") == null || "".equals(list.get(0).get("bh"))) {
				this.entity.setSerial_number(sysdate + "0001");			
			} else {
				if(this.entity.getSerial_number()==null){
					//System.out.println("this.entity.getBh()=="+this.entity.getBh());
					
				this.entity.setSerial_number(String.valueOf(Long.parseLong((String)list.get(0).get("bh")) + 1 ));
				}
			}
			//从工作任务单中提取信息
			if(!StringUtils.isNullOrBlankFull(id)) {
				TGzrwd tGzrwd = (TGzrwd)workcardService.findById(Long.parseLong(id), TGzrwd.class.getName());//取得工作任务单pojo
				this.entity.setOc_gzph(tGzrwd.getGzpno()); 	//设置票号
				this.entity.setOc_ecgzaqcsph("");			//设置二次工作安全措施票号			
				TUser tUser=(TUser)workcardService.findById(Long.parseLong(tGzrwd.getZbz().trim()), TUser.class.getName());//取得班组pojo
				this.entity.setOc_gzfzr(tUser.getName());	//设置工作负责人
				this.entity.setOc_cy(tGzrwd.getZbyname());	//设置成员
				TOrgan tOrgan=(TOrgan)workcardService.findById(Long.parseLong(tGzrwd.getBgzr().trim()), TOrgan.class.getName());//取得班组pojo
				this.entity.setOc_gzbz(tOrgan.getOrganName());	//设置工作班组
				this.entity.setDt_jhgzsjq(tGzrwd.getCzkssj());//设置计划工作时间起
				this.entity.setDt_jhgzsjz(tGzrwd.getCzjssj());//设置票号计划工作时间止
			}
			if(entity!=null&&"".equals(entity.getOc_bxr())||entity.getOc_bxr()==null){
				TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);		
				entity.setOc_bxr(tUser.getName());
				entity.setCjrid(tUser.getId());
			}
			entity.setPlanid(planid);
		}
		return "UPDATE_SUCCESS";
	}	
	/**
	 * 获得系统时间和负责人
	 * @throws IOException 
	 */
	public void getSysdateAndFzr() throws IOException{
		String sysdate = workcardService.getSysdate("yyyy-MM-dd HH24:mi");
		TUser sessionUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);	
		String fzr=sessionUser.getName();
		super.getResponse().getWriter().print("{sysdate:'"+sysdate+"',fzr:'"+fzr+"'}");
	}
	/**
	 * 批量删除工序质量控制卡
	 * @return
	 */	
	public String deleteGxzlkzk(){
		if(!StringUtils.isNullOrBlankFull(id)) {
			workcardService.deleteByDHql(" p_idsno in("+id+")",null,Gxzlkzk_BZ.class.getName());
			workcardService.doDelAll(id,Gxzlkzk.class.getName());		
		}		
		return "DELETE_SUCCESS";
	}
	/**
	 * 批量审核工序质量控制卡
	 * @return
	 */
	public String verifyGxzlkzk_PL(){
		if(!StringUtils.isNullOrBlankFull(id)) {
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);		
			workcardService.update(" oc_status='1',oc_spr='"+tUser.getName()+"'"," id_sno in ("+id+")",Gxzlkzk.class.getName());		
		}
		return "SH_SUCCESS_PL";
	}	
	/**
	 * 审核工序质量控制卡
	 * @return
	 */
	public String verifyGxzlkzk(){
		if(!StringUtils.isNullOrBlankFull(id)) {
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);
			workcardService.update(" oc_status='1',oc_spr='"+tUser.getName()+"'"," id_sno='"+id+"'",Gxzlkzk.class.getName());		
		}
		String info="审核成功!";
		super.setInfo(info);
		return "SH_SUCCESS";
	}	
	/**
	 * 另存为典型工序质量控制卡模版
	 * @return
	 */
	public String updateModel(){
		if(!StringUtils.isNullOrBlankFull(id)) {
			TUser tUser=(TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);
			workcardService.update(" model='1'"," id_sno='"+id+"'",Gxzlkzk.class.getName());		
		}
		String info="另存为典型工序质量控制卡模版成功!";
		super.setInfo(info);
		return "SH_SUCCESS";
	}	
	/**
	 * 保存标准作业工序
	 * @return
	 * @throws AppException
	 */	
	public void saveGx() throws IOException, AppException {
		
		Gxzlkzk_GX gx = new Gxzlkzk_GX();
		gx.setOc_gx(ocGx);
		gx.setOc_gybzjyq(ocGybzjyq);
		gx.setOc_fxtx(ocFxtx);
		gx.setSortId(sortId);
		gx.setParentId(Long.parseLong(parentId));
		
		// 新建
		if ("".equals(id) || "0".equals(id)) {
			//m.setId(SeqUtil.getNextKey("SEQ_SYS"));
			this.workcardService.save(gx);
			
			// 修改
		} else {
			gx.setId(Long.parseLong(id));
			this.workcardService.update(gx);
		}

		super.getResponse().getWriter().print("{success:true}");
	}
	/**
	 * 验证
	 * @return
	 * @throws AppException
	 * @throws IOException 
	 */
	public void check() throws Exception {
		
		String out = "{}";
		
		// 验证主键ID
		long count = workcardService.getTotalByDHql(
				"parentId = ? " , 
				new Object[]{Long.parseLong(parentId)},
				"Gxzlkzk_GX");
		count=0;//永远不严重主键ID不可能重复
		if (count == 1) {
			out = "{success:true,m:'工序主键重复请重新输入！'}";
		}

		super.getResponse().getWriter().print(out);
		
	}
	/**
	 * 删除一条记录（工序）
	 * @return
	 * @throws AppException
	 */	
	public void delete() throws IOException, AppException {
		
		//this.workcardService.delete(Long.parseLong(id));
		workcardService.doDelAll(id,Gxzlkzk_GX.class.getName());	
		super.getResponse().getWriter().print("{success:true}");
	}
	/**
	 * 删除一条记录（班组）
	 * @return
	 * @throws AppException
	 */	
	public void deleteBz() throws IOException, AppException {
		
		//this.workcardService.delete(Long.parseLong(id));
		workcardService.doDelAll(id,Gxzlkzk_BZ.class.getName());	
		super.getResponse().getWriter().print("{success:true}");
	}
	public WorkcardService getWorkcardService() {
		return workcardService;
	}
	public void setWorkcardService(WorkcardService workcardService) {
		this.workcardService = workcardService;
	}
	public Gxzlkzk getEntity() {
		return entity;
	}
	public void setEntity(Gxzlkzk entity) {
		this.entity = entity;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<Gxzlkzk_BZ> getGxzlkzkBZList() {
		return gxzlkzkBZList;
	}
	public void setGxzlkzkBZList(List<Gxzlkzk_BZ> gxzlkzkBZList) {
		this.gxzlkzkBZList = gxzlkzkBZList;
	}
	public String getDirectFlag() {
		return directFlag;
	}
	public void setDirectFlag(String directFlag) {
		this.directFlag = directFlag;
	}
	public String getStart() {
		return start == null ? "0" : start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit == null ? IConstant.PAGE_LIMIT : limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getOcGx() {
		return ocGx;
	}

	public void setOcGx(String ocGx) {
		this.ocGx = ocGx;
	}

	public String getOcGybzjyq() {
		return ocGybzjyq;
	}

	public void setOcGybzjyq(String ocGybzjyq) {
		this.ocGybzjyq = ocGybzjyq;
	}

	public String getOcFxtx() {
		return ocFxtx;
	}

	public void setOcFxtx(String ocFxtx) {
		this.ocFxtx = ocFxtx;
	}
	public void setSortId(String sortId) {
		this.sortId = sortId;
	}

	public String getBzgsids() {
		return bzgsids;
	}

	public void setBzgsids(String bzgsids) {
		this.bzgsids = bzgsids;
	}

	public String getSortId() {
		return sortId;
	}

	public List<Gxzlkzk_GX> getGxzlkzkGXList() {
		return gxzlkzkGXList;
	}

	public void setGxzlkzkGXList(List<Gxzlkzk_GX> gxzlkzkGXList) {
		this.gxzlkzkGXList = gxzlkzkGXList;
	}
	public String getModelids() {
		return modelids;
	}
	public void setModelids(String modelids) {
		this.modelids = modelids;
	}
	
}
